#ifndef CMAINBOARDRECEIVER_H
#define CMAINBOARDRECEIVER_H

#include <QObject>
#include <QHostAddress>
#include <QStringList>
#include <QMutex>

#include "dataitemdef.h"

class QUdpSocket;


class CMainBoardReceiver : public QObject
{
    Q_OBJECT
public:
    explicit CMainBoardReceiver(QObject *parent = nullptr);

    int Initialize();
    void Invalidate();

    // $START,<2048khz>,<TTL>,<RS422>,<TOD>,<BAC>,<PTP>,<NTP>,<10M><CR><LF>
    int StartTesting( bool b2048KHz, bool bTTL, bool bRS422, bool bToD, bool bBAC, bool bPTP, bool bNTP, bool b10MHz );
    void WriteData( const QByteArray & aData );

signals:
    void signal_GetDataFromMainboard();
    void signal_SysInfoData( const TMB_SYSINFO& SysInfo );
    void signal_IOSBV( const TMB_IOSBV& IoSBV );
    void signal_SIGTIE( const TMB_SIGTIE & SIGTIE );
    void signal_TTLTIE( const TMB_SIGTIE & SIGTIE );
    void signal_ToDTIE( const TMB_TOD_BDC_BAC & testValue );
    void signal_BDCTIE( const TMB_TOD_BDC_BAC & testValue );
    void signal_BACTIE( const TMB_TOD_BDC_BAC & testValue );
    void signal_NTPTIE( const TMB_NTP_PTP & testValue );
    void signal_PTPTIE( const TMB_NTP_PTP & testValue );
    void signal_10MHz( const TMB_10MHz & testValue );
    void signal_CmdResponse( QString strCmd, QStringList astrCmdResponse );

protected slots:
    void processPendingDatagrams();

protected:
    int CreateUdpSocket();
    void ProcessInputData();
    void ClearLastData();
    void ProcessInputData_OneLine( QByteArray strOneLine );
    void ProcessInputData_SysInfo( const QStringList & astrData );
    void ProcessInputData_IOSBV( const QStringList & astrData );
    void ProcessInputData_SIGTIE( const QStringList & astrData );
    void ProcessInputData_TTLTIE( const QStringList & astrData );
    void ProcessInputData_TODTIE( const QStringList & astrData );
    void ProcessInputData_BDCTIE( const QStringList & astrData );
    void ProcessInputData_BACTIE( const QStringList & astrData );
    void ProcessInputData_NTPTIE( const QStringList & astrData );
    void ProcessInputData_PTPTIE( const QStringList & astrData );
    void ProcessInputData_XFREQ( const QStringList & astrData );
    void TestParser();

protected:
    bool GetBoolValue( QString strValue );
    int GetTod_BDC_BAC_Data( const char * pszTitle, const QStringList & astrData, TMB_TOD_BDC_BAC & outData );
    uint32_t GetDateValue( QString strValue );
    uint32_t GetTimeValue( QString strValue );
    int GetNTP_PTPData( const char * pszTitle, const QStringList & astrData, TMB_NTP_PTP & outData );


protected:
    int                         m_nDstPort;
    QHostAddress                m_DstAddr;
    int                         m_nLocalPort;
    QMutex                      m_SyncObj;
    QUdpSocket              *   m_pUdpSocket = nullptr;
    QByteArray                  m_aInDataCache;

    TMB_SYSINFO                 m_lastSysInfo;
    TMB_IOSBV                   m_lastIOSBV;
    TMB_SIGTIE                  m_lastSigTie;
    TMB_TOD_BDC_BAC             m_lastToD;
    TMB_TOD_BDC_BAC             m_lastBDC;
    TMB_TOD_BDC_BAC             m_lastBAC;
    TMB_NTP_PTP                 m_lastNTP;
    TMB_NTP_PTP                 m_lastPTP;
    TMB_10MHz                   m_last10MHz;

};

#endif // CMAINBOARDRECEIVER_H
